package cn.codergege.kotlinbasics.controlflow

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class ReturnDemoTest {

    fun foo(): ArrayList<Int> {
        val list = ArrayList<Int>()
        (1..5).forEach lable@{
            if(it == 3) return@lable
            list.add(it) // list 里应该是 1, 2, 4, 5
        }
        return list
    }

    @Test fun testReturn() {
        val result = foo()
        assertEquals(4, result.size)
        assertTrue(result[0] == 1)
    }

    /**
     *  或者直接用隐式标签
     */
    fun bar(): ArrayList<Int> {
        val list = ArrayList<Int>()
        (1..5).forEach {
            if(it == 3) return@forEach // 与 forEach 同名
            list.add(it) // list 里应该是 1, 2, 4, 5
            println(it)
        }
        return list
    }
    @Test fun testReturnImpl() {
        val result = bar()
        assertEquals(4, result.size)
        assertTrue(result[1] == 2)
    }
}